package controller;

import entity.Order;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import util.ExcelUtil;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/excel")
public class PoiExcelController {
    @RequestMapping(value="/exportOrder")
    public String download(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入download");
        String fileName="excel文件";
        //填充projects数据
        List<Order> orders=createData();
        List<Map<String,Object>> list=createExcelRecord(orders);
        String columnNames[]={"订单号","用户id","订单编号","下单时间","总价","订单状态"};//列名
        String keys[]    =   {"订单号","用户id","订单编号","下单时间","总价","订单状态"};//map中的key
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            ExcelUtil.createWorkBook(list,keys,columnNames).write(os);
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] content = os.toByteArray();
        InputStream is = new ByteArrayInputStream(content);
        // 设置response参数，可以打开下载页面
        response.reset();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename="+ new String((fileName + ".xls").getBytes(), "iso-8859-1"));
        ServletOutputStream out = response.getOutputStream();
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(is);
            bos = new BufferedOutputStream(out);
            byte[] buff = new byte[2048];
            int bytesRead;
            // Simple read/write loop.
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        } catch (final IOException e) {
            throw e;
        } finally {
            if (bis != null)
                bis.close();
            if (bos != null)
                bos.close();
        }
        return null;
    }
    private List<Order> createData() {
        // TODO Auto-generated method stub
        //自己实现

        List<Order> list=new ArrayList<>();
        list.add(new Order(1,2,100.0,"2022","1"));
        list.add(new Order(2,5,200.0,"2022","2"));
        list.add(new Order(3,8,300.0,"2022","3"));

        return list;
    }
    private List<Map<String, Object>> createExcelRecord(List<Order> list) {
        List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("sheetName", "sheet1");
        listmap.add(map);
        Order order=null;
        for (int j = 0; j < list.size(); j++) {
            order=list.get(j);
            Map<String, Object> mapValue = new HashMap<String, Object>();
            mapValue.put("订单号", order.getOid());
            mapValue.put("用户id", order.getUserId());
            mapValue.put("订单编号", order.getOrdernum());
            mapValue.put("下单时间", order.getOrderTime());
            mapValue.put("总价", order.getTotalPrice());
            mapValue.put("订单状态", order.getOrderStatus());

            listmap.add(mapValue);
        }
        return listmap;
    }

}
